/*
 *  SVO - Simple Video Out FPGA Core
 *
 *  Copyright (C) 2014  Clifford Wolf <clifford@clifford.at>
 *  
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

`timescale 1ns / 1ps
`include "svo_defines.vh"

module svo_tcard #( `SVO_DEFAULT_PARAMS ) (
	input clk, resetn,

	// output stream
	//   tuser[0] ... start of frame
	input [1:0] xypos,
	input [1:0] direct,
	input [8:0] axis_xpos,
	input [8:0] axis_ypos,
	output reg out_axis_tvalid,
	input out_axis_tready,
	output reg [SVO_BITS_PER_PIXEL-1:0] out_axis_tdata,
	output reg [0:0] out_axis_tuser
);
	`SVO_DECLS

	localparam HOFFSET = ((32 - (SVO_HOR_PIXELS % 32)) % 32) / 2;//
	localparam VOFFSET = ((32 - (SVO_VER_PIXELS % 32)) % 32) / 2;// 

	localparam HOR_CELLS = (SVO_HOR_PIXELS + 31) / 32;
	localparam VER_CELLS = (SVO_VER_PIXELS + 31) / 32;

	//localparam BAR_W = (HOR_CELLS - 8 - HOR_CELLS%2) / 2;

	reg [`SVO_XYBITS-1:0] hcursor;
	reg [`SVO_XYBITS-1:0] vcursor;

	reg [`SVO_XYBITS-6:0] x;
	reg [`SVO_XYBITS-6:0] y;

//	reg [4:0] axis_xpos;
//	reg [4:0] axis_ypos;

	reg [4:0] xoff, yoff;

	reg [31:0] rng;
	reg [SVO_BITS_PER_RED-1:0] r;
	reg [SVO_BITS_PER_GREEN-1:0] g;
	reg [SVO_BITS_PER_BLUE-1:0] b;

wire [32*32-1:0] bolt_bitmap_1 = {
		32'b 00000000000000000000000000000000,
		32'b 01111111111111111111111111111111,
		32'b 00111111111111111111111111111111,
		32'b 00011111111111111111111111111111,
		32'b 00001111111111111111111111111111,
		32'b 00000111111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000001111111111111111111111111,
		32'b 00000000111111111111111111111111,
		32'b 00000000011111111111111111111111,
		32'b 00000000001111111111111111111111,
		32'b 00000000000111111111111111111111,
		32'b 00000000000011111111111111111111,
		32'b 00000000000001111111111111111111,
		32'b 00000000000000111111111111111111,
		32'b 00000000000000011111111111111111,
		32'b 00000000000000001111111111111111,
		32'b 00000000000000000111111111111111,
		32'b 00000000000000000011111111111111,
		32'b 00000000000000000001111111111111,
		32'b 00000000000000000000111111111111,
		32'b 00000000000000000000011111111111,
		32'b 00000000000000000000001111111111,
		32'b 00000000000000000000000111111111,
		32'b 00000000000000000000000011111111,
		32'b 00000000000000000000000001111111,
		32'b 00000000000000000000000000111111,
		32'b 00000000000000000000000000011111,
		32'b 00000000000000000000000000001111,
		32'b 00000000000000000000000000000111,
		32'b 00000000000000000000000000000011,
		32'b 00000000000000000000000000000001
	};

wire [32*32-1:0] bolt_bitmap_2 = {
		32'b 00000000000000000000000000000000,
		32'b 01111111111111111111111111111111,
		32'b 01111111111111111111111111111110,
		32'b 01111111111111111111111111111100,
		32'b 01111111111111111111111111111000,
		32'b 01111111111111111111111111110000,
		32'b 01111111111111111111111111100000,
		32'b 01111111111111111111111111000000,
		32'b 01111111111111111111111110000000,
		32'b 01111111111111111111111100000000,
		32'b 01111111111111111111111000000000,
		32'b 01111111111111111111110000000000,
		32'b 01111111111111111111100000000000,
		32'b 01111111111111111111000000000000,
		32'b 01111111111111111110000000000000,
		32'b 01111111111111111100000000000000,
		32'b 01111111111111111000000000000000,
		32'b 01111111111111110000000000000000,
		32'b 01111111111111100000000000000000,
		32'b 01111111111111000000000000000000,
		32'b 01111111111110000000000000000000,
		32'b 01111111111100000000000000000000,
		32'b 01111111111000000000000000000000,
		32'b 01111111110000000000000000000000,
		32'b 01111111100000000000000000000000,
		32'b 01111111000000000000000000000000,
		32'b 01111110000000000000000000000000,
		32'b 01111100000000000000000000000000,
		32'b 01111000000000000000000000000000,
		32'b 01110000000000000000000000000000,
		32'b 01100000000000000000000000000000,
		32'b 01000000000000000000000000000000
	};

wire [32*32-1:0] bolt_bitmap = {
		32'b 00000000000000000000000000000000,
		32'b 01111111000000000000000001111111,
		32'b 01111100000000000000000000011111,
		32'b 01110000000000000000000000000111,
		32'b 01100000000000000000000000000011,
		32'b 01100000000000000000000000000011,
		32'b 01000000000000000000000000000001,
		32'b 01000000000000000000000000000001,
		32'b 00000000000000000000000000000000,
		32'b 00000000000000000000000000000000,
		32'b 00000000000000000000000000000000,
		32'b 00000000000000000000000000000000,
		32'b 00000000000000111100000000000000,
		32'b 00000000000001111110000000000000,
		32'b 00000000000011111111000000000000,
		32'b 00000000000011111111000000000000,
		32'b 00000000000011111111000000000000,
		32'b 00000000000011111111000000000000,
		32'b 00000000000001111110000000000000,
		32'b 00000000000000111100000000000000,
		32'b 00000000000000000000000000000000,
		32'b 00000000000000000000000000000000,
		32'b 00000000000000000000000000000000,
		32'b 00000000000000000000000000000000,
		32'b 00000000000000000000000000000000,
		32'b 01000000000000000000000000000001,
		32'b 01000000000000000000000000000001,
		32'b 01100000000000000000000000000011,
		32'b 01100000000000000000000000000011,
		32'b 01110000000000000000000000000111,
		32'b 01111100000000000000000000011111,
		32'b 01111111000000000000000001111111
	};

wire [32*32-1:0] tank_bitmap_U = {
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111
	};
wire [32*32-1:0] tank_bitmap_L = {
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111,
		32'b 00000011111111111111111111111111
	};
wire [32*32-1:0] tank_bitmap_D = {
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000,
		32'b 00000000000111111111100000000000
	};
wire [32*32-1:0] tank_bitmap_R = {
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111111111,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000,
		32'b 11111111111111111111111111000000
	};
	always @(posedge clk) begin
		if (!resetn) begin
			hcursor <= 0;
			vcursor <= 0;
			x <= 0;
			y <= 0;
//			axis_xpos<=17;
//			axis_ypos<=10;
			xoff <= HOFFSET;
			yoff <= VOFFSET;
			out_axis_tvalid <= 0;
			out_axis_tdata <= 0;
			out_axis_tuser <= 0;
		end else
		if (!out_axis_tvalid || out_axis_tready) begin
			if (!xoff || hcursor == 0) begin
				r = 0;//16 * rng[0] + 16 * rng[1] + 31 * rng[2];
				g = 0;//16 * rng[3] + 16 * rng[4] + 31 * rng[5];
				b = 0;//16 * rng[6] + 16 * rng[7] + 31 * rng[8];//这里原本画的小方块

				if ({r, g, b} == 0) begin
					r = 0;//32
					g = 0;//32
					b = 0;//32//这个是最左边那一栏
				end
			end

			if (&xoff || &yoff) begin
				r = 63;
				g = 63;
				b = 63;//这个画的边框
			end
//原本画小方块的位置
			out_axis_tvalid <= 1;

			if ((x == 1 || x == HOR_CELLS-2) && (y == 1 || y == VER_CELLS-2))
				out_axis_tdata <= bolt_bitmap[{yoff,  xoff}] ? ~0 : 0;
			else if((x==axis_xpos)&&(y==axis_ypos)) begin
				case(direct)
					2'b00:begin out_axis_tdata <= tank_bitmap_D[{yoff,  xoff}] ? ~0 : 0; end
					2'b01:begin out_axis_tdata <= tank_bitmap_L[{yoff,  xoff}] ? ~0 : 0; end
					2'b10:begin out_axis_tdata <= tank_bitmap_U[{yoff,  xoff}] ? ~0 : 0; end
					2'b11:begin out_axis_tdata <= tank_bitmap_R[{yoff,  xoff}] ? ~0 : 0; end
				endcase
			end
			else
				out_axis_tdata <= {b, g, r};
			out_axis_tuser[0] <= !hcursor && !vcursor;

			if (hcursor == SVO_HOR_PIXELS-1) begin
				hcursor <= 0;
				x <= 0;
				xoff <= HOFFSET;
				if (vcursor == SVO_VER_PIXELS-1) begin
					vcursor <= 0;
					y <= 0;
					yoff <= VOFFSET;
				end else begin
					vcursor <= vcursor + 1;
					if (&yoff)
						y <= y + 1;
					yoff <= yoff + 1;
				end
			end else begin
				hcursor <= hcursor + 1;
				if (&xoff)
					x <= x + 1;
				xoff <= xoff + 1;
			end
		end
	end

endmodule